<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Mar 28, 2012
 * Time: 3:35:43 AM
 */
class Menu {
    private $db;

    public function __construct() {
        $this->db = new MysqlImprovedDriver;
    }

    public function get_menu_xml($parent_id, $top_id) {

        $this->db->connect();
        if (!isset($parent_id)) {
            //top level
            $xml = '<?xml version="1.0"?><menu>';
            $parent_id = "top";
        } else {
            // sublevel
            $xml = '<?xml version="1.0"?><menu parentId="' . $parent_id . '">';
        }
        $parent_id = $this->db->escape($parent_id);
        $this->db->prepare("SELECT `a`.`item_id`, `a`.`item_text`, `a`.`item_type`, `a`.`item_enabled`, `a`.`item_permission`,`a`.`item_checked`, `a`.`item_group`,`a`.`item_image`,`a`.`item_url`, `a`.`item_image_dis`, COUNT(`b`.`item_id`) AS `itemComplex` FROM `aea_menu` AS `a` LEFT JOIN (`aea_menu` AS `b`) ON (`b`.`item_parent_id`=`a`.`item_id`) WHERE `a`.`item_parent_id`='" . $parent_id . "' GROUP BY `a`.`item_id` ORDER BY `a`.`id`");
        $this->db->query();
        $permission = $_SESSION['permission'];
        //print_r($permission);
        while ($out = $this->db->fetch()) {
            if (isset($permission[$out->item_permission]) && $permission[$out->item_permission] == 1) {
                $xml = $xml . '<item id="' . $out->item_id . '" text="' . $out->item_text . '"' .
                        (strlen($out->item_type) > 0 ? ' type="' . $out->item_type . '"' : '') .
                        ($out->item_enabled == "0" ? ' enabled="false"' : '') .
                        ($out->item_checked == "1" ? ' checked="true"' : '') .
                        (strlen($out->item_group) > 0 ? ' group="' . $out->item_group . '"' : '') .
                        (strlen($out->item_image) > 0 ? ' img="' . $out->item_image . '"' : '') .
                        (strlen($out->item_image_dis) > 0 ? ' imgdis="' . $out->item_image_dis . '"' : '') .
                        ($out->itemComplex > 0 ? ' complex="true"' : '') .
                        '> <userdata name="url">?' . $out->item_url . '</userdata></item>';
            }
        }

        $xml = $xml . '</menu>';
        return $xml;
    }


}
 
